package cn.wolfcode.rbac.controller;

import cn.wolfcode.rbac.common.RequirePermission;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.domain.query.EmployeeQueryObject;
import cn.wolfcode.rbac.domain.vo.AdminStateVo;
import cn.wolfcode.rbac.domain.vo.EmployeeRoleVo;
import cn.wolfcode.rbac.domain.vo.PageResult;
import cn.wolfcode.rbac.domain.vo.R;
import cn.wolfcode.rbac.service.IEmployeeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/employee")
@CrossOrigin(allowCredentials = "true")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @GetMapping("/list")
    @ResponseBody
    @RequirePermission({"员工列表","employee:list"})
    public R list(EmployeeQueryObject qo) {
        IPage<Employee> employeeIPage = employeeService.selectEmployeeByPage(qo);
        return R.ok(employeeIPage);
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    @RequirePermission({"员工删除","employee:delete"})
    public R delete(@PathVariable Long id) {
        employeeService.removeById(id);
        return R.ok("删除成功");
    }

    @GetMapping("/info/{id}")
    @ResponseBody
    @RequirePermission({"员工信息","employee:info"})
    public R info(@PathVariable Long id ) {
        Employee employee;
        try {
            employee = employeeService.getById(id);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok(employee);
    }

    @PostMapping("/saveOrUpdate")
    @ResponseBody
    @RequirePermission({"员工保存或更新","employee:saveOrUpdate"})
    public R saveOrUpdate(@RequestBody EmployeeRoleVo employeeRoleVo) {
        try {
            employeeService.saveOrUpdateEmployee(employeeRoleVo);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok("添加或修改成功");
    }

    @PostMapping("/updateState")
    @ResponseBody
    @RequirePermission({"更新管理员","employee:updateState"})
    public R updateState(@RequestBody AdminStateVo adminStateVo){
        try {
            employeeService.updateStateById(adminStateVo);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.ok("更新状态成功");
    }
}
